<template>
  <el-card>
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">
      <span style="font-size: 18px; font-weight: bold;">薪资管理</span>
      <el-button type="primary" @click="openDialog()">新增薪资</el-button>
    </div>
    <el-table :data="salaries" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="account" label="薪资账号" width="160" />
      <el-table-column prop="amount" label="金额" width="120" />
      <el-table-column prop="payMonth" label="发薪月份" width="120" />
      <el-table-column prop="user.idCard" label="员工身份证号" width="140" />
      <el-table-column prop="user.name" label="员工姓名" width="100" />
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button size="small" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteSalary(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="薪资账号" prop="account">
          <el-input v-model="form.account" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input-number v-model="form.amount" :min="0" />
        </el-form-item>
        <el-form-item label="发薪月份" prop="payMonth">
          <el-date-picker v-model="form.payMonth" type="month" placeholder="选择月份" format="YYYY-MM" value-format="YYYY-MM" />
        </el-form-item>
        <el-form-item label="员工" prop="userId">
          <el-select v-model="form.userId" filterable placeholder="请选择员工">
            <el-option v-for="u in users" :key="u.id" :label="u.name + '（' + u.idCard + '）'" :value="u.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import api from '../api'
import { ElMessage } from 'element-plus'

const salaries = ref([])
const users = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const form = ref({})
const formRef = ref()

const rules = {
  account: [{ required: true, message: '请输入薪资账号', trigger: 'blur' }],
  amount: [{ required: true, message: '请输入金额', trigger: 'blur' }],
  payMonth: [{ required: true, message: '请选择月份', trigger: 'change' }],
  userId: [{ required: true, message: '请选择员工', trigger: 'change' }]
}

function fetchSalaries() {
  api.get('/salaries').then(res => {
    salaries.value = res.data
  })
}
function fetchUsers() {
  api.get('/users').then(res => {
    users.value = res.data
  })
}

function openDialog(row) {
  dialogTitle.value = row ? '编辑薪资' : '新增薪资'
  if (row) {
    form.value = {
      id: row.id,
      account: row.account,
      amount: row.amount,
      payMonth: row.payMonth,
      userId: row.user?.id
    }
  } else {
    form.value = {}
  }
  dialogVisible.value = true
}

function submitForm() {
  formRef.value.validate(valid => {
    if (!valid) return
    const payload = {
      ...form.value,
      user: { id: form.value.userId }
    }
    if (form.value.id) {
      api.put(`/salaries/${form.value.id}`, payload).then(() => {
        ElMessage.success('修改成功')
        dialogVisible.value = false
        fetchSalaries()
      })
    } else {
      api.post('/salaries', payload).then(() => {
        ElMessage.success('新增成功')
        dialogVisible.value = false
        fetchSalaries()
      })
    }
  })
}

function deleteSalary(id) {
  api.delete(`/salaries/${id}`).then(() => {
    ElMessage.success('删除成功')
    fetchSalaries()
  })
}

onMounted(() => {
  fetchSalaries()
  fetchUsers()
})
</script> 